<?php

namespace app\api\controller;

use app\common\controller\Api;

use think\Validate;
use app\common\library\Token;
use fast\Random;
use think\Db;
/**
 * 示例接口
 */
class Demo extends Api
{
    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['test', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2','tags'];
    protected $uid = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->uid = $this->auth->id;
    }
    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test()
    {
        $rule = [
            'openid'  => 'require|length:10,30'
        ];
        
        $msg = [
            'openid.require' => '缺少openid',
            'openid.length' => 'openid长度不符合',
        ];

        $v = new Validate($rule,$msg);

        $post=$this->request->param();
        
        if (!$v->check($post)) {
            $this->error('登录失败：' . $v->getError());
        }
        $u = model('admin/User')->where('openid',$post["openid"])->find();
        if($u){
            Token::clear($u["id"]);
            $this->auth->direct($u["id"]);
            $this->success('登录成功', $this->auth->getUserinfo());
        }
        else{
            $username = $post["openid"];
            // 初始密码给一个随机数
            $password = Random::alnum(15);
            $this->auth->register($username,$password,'','',[
                "openid"=>$post["openid"]
            ]);
            $this->success('注册成功', $this->auth->getUserinfo());
        }
    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $todo = model('admin/Todo');
        // $todo ->allowField(true)->save([
        //     "userid"=>$this->uid,
        //     "tags"=>"其他",
        //     "task"=>"测试3331232233",
        // ]);
        
        $where=[
            "userid"=>$this->uid,
        ];
        $list = $todo->where($where)->group("tags")->select();
        $this->success('返回成功', $list);
    }

    public function tags()
    {
        $tags = input("tags");
        $where=[
            "userid"=>$this->uid,
        ];
        
        $list = model('admin/Todo')
        ->where($where);
        if($tags){
            $list=$list->where('','exp', "instr(CONCAT( ',', tags, ',' ),  ',".$tags.",' )");
        }
        
        $list = $list->select();
        $this->success('返回成功', $list);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

}
